package com.crm.service.impl;

import com.crm.Mapper.CluesMapper;
import com.crm.Mapper.FollowMapper;
import com.crm.entity.Clue;
import com.crm.entity.Follow;
import com.crm.page.CluePage;
import com.crm.service.ClueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private CluesMapper cluesMapper;
    @Autowired
    private FollowMapper followMapper;
    @Override
    //数据一致性
    @Transactional
    public void insertClue(Clue clue) {
//        clue.setUserid();//默认当前登陆用户
        Long maxId = cluesMapper.selectMaxId();
        if (maxId == null){
            maxId = 1L;
        }else {
            maxId +=1L;
        }
        clue.setId(maxId);//补充id
        clue.setStatus(1);//默认1 待跟进
        clue.setFaketype(0);//默认0 不为伪线索
        cluesMapper.insertClue(clue);
        Follow follow = new Follow();
        Long maxFollowId = followMapper.selectMaxId();
        if (maxFollowId == null){
            maxFollowId = 1L;
        }else {
            maxFollowId +=1L;
        }
        follow.setId(maxFollowId);
        follow.setClueid(clue.getId());
        follow.setCreateBy(clue.getId());
        followMapper.insertFollow(follow);

    }

    @Override
    public PageInfo<Clue> getClueListByPage(CluePage page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());

        List<Clue> clues = cluesMapper.selectCluesByCondition(page.getBeginCreateTime(), page.getEndCreateTime());
        return new PageInfo<>(clues);
    }
}
